﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEditorInternal;
using UnityEngine;

namespace XFFSM
{
    [CustomEditor(typeof(FSMStateInspectorHelper))]
    public class FSMStateInspector : Editor
    {
        //private string stateName;

        private ReorderableList reorderableList;

        private void OnEnable()
        {
            FSMStateInspectorHelper helper = target as FSMStateInspectorHelper;
            if (helper == null) { return; }

            reorderableList = new ReorderableList(helper.node.StateScripts, typeof(FSMStateScriptInfo), true, true, true, true);
            reorderableList.drawHeaderCallback += OnDrawHeaderCallback;
            //reorderableList.onAddCallback += this.OnAddCallback;
            //reorderableList.onRemoveCallback += this.OnRemoveCallback;
            reorderableList.drawElementCallback += this.DrawItem;
        }

        public override void OnInspectorGUI()
        {
            //base.OnInspectorGUI();

            FSMStateInspectorHelper helper = target as FSMStateInspectorHelper;
            if (helper == null) return;

            bool disabled = EditorApplication.isPlaying
                || helper.node.name.Equals(FSMConst.anyState)
                || helper.node.name.Equals(FSMConst.entryState);

            EditorGUI.BeginDisabledGroup(disabled);

            reorderableList.list = helper.node.StateScripts;
            reorderableList.DoLayoutList();

            helper.controller.Save();

            EditorGUI.EndDisabledGroup();
        }

        protected override void OnHeaderGUI()
        {
            //base.OnHeaderGUI();
            FSMStateInspectorHelper helper = target as FSMStateInspectorHelper;
            if (helper == null) return;


            string name = null;
            EditorGUI.BeginChangeCheck();
            {
                EditorGUILayout.Space();
                EditorGUILayout.BeginHorizontal();

                GUILayout.Label(EditorGUIUtility.IconContent("icons/processed/unityeditor/animations/animatorstate icon.asset"), GUILayout.Width(30), GUILayout.Height(30));

                EditorGUILayout.LabelField("Name:", GUILayout.Width(60));

                bool disabled = EditorApplication.isPlaying
                    || helper.node.name.Equals(FSMConst.anyState)
                    || helper.node.name.Equals(FSMConst.entryState);

                EditorGUI.BeginDisabledGroup(disabled);
                name = EditorGUILayout.DelayedTextField(helper.node.name);
                EditorGUI.EndDisabledGroup();

                EditorGUILayout.EndHorizontal();
            }
            if (EditorGUI.EndChangeCheck())
            {
                //Debug.Log("重命名:TODO");
                FSMStateNodeFactory.Rename(helper.controller, helper.node, name);
            }
            EditorGUILayout.Space();
            var rect = EditorGUILayout.BeginHorizontal();

            Handles.color = Color.black;
            Handles.DrawLine(new Vector2(rect.x, rect.y), new Vector3(rect.x + rect.width, rect.y));

            EditorGUILayout.EndHorizontal();
            EditorGUILayout.Space();
        }


        //private void OnAddCallback(ReorderableList list)
        //{
        //    list.list.Add(string.Empty);
        //    //FSMTransitionInspectorHelper helper = target as FSMTransitionInspectorHelper;
        //    //if (helper == null) { return; }
        //    ////Debug.Log("添加条件");
        //    //FSMConditionFactory.CreateCondition(helper.controller, helper.transition);
        //}

     
        private void DrawItem(Rect rect, int index, bool isActive, bool isFocused)
        {
             

            if (index < 0 || index >= reorderableList.list.Count) return;
            FSMStateScriptInfo info = reorderableList.list[index] as FSMStateScriptInfo;
            if (info == null) return;
            info.className = GUI.TextField(rect, info.className);
            if (string.IsNullOrEmpty(info.className)) 
            {
                EditorGUI.BeginDisabledGroup(true);
                GUI.Label(rect, "请输入类的全名(含命名空间)!");
                EditorGUI.EndDisabledGroup();
            }
        }

        private void OnDrawHeaderCallback(Rect rect)
        {
            GUI.Label(rect, "State Scripts");
        }

    }


}

